package com.example.musicplayer

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MusicRecommendation:Fragment() {

    private val bannerImageURIList= mutableListOf<String>()
    private val recDataList=ArrayList<rec>()

    private lateinit var TitleImage:ImageView

    private lateinit var RecommendedSong:RecyclerView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view=inflater.inflate(R.layout.music_pecommendation,container,false)
        TitleImage=view.findViewById(R.id.TitleImage)
        RecommendedSong=view.findViewById(R.id.RecommendedSong)
        return view
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val JSONBannerService=ServiceCreator.create(JSONImageService::class.java)
        JSONBannerService.getBannerImage().enqueue(object :Callback<List<banner>> {
            override fun onResponse(call: Call<List<banner>>, response: Response<List<banner>>) {
                val list=response.body()
                if (list!=null) {
                    for (banner in list) {
//                        Log.d("MainActivity","id is ${banner.id}")
                        Log.d("MainActivity","bannerImageUrl is ${banner.bannerImgUrl}")
                        bannerImageURIList.add(banner.bannerImgUrl)

                    }
//                    Glide.with(this@MusicRecommendation)
//                        .load(bannerImageURIList[2])
//                        .into(TitleImage)
                }
            }

            override fun onFailure(p0: Call<List<banner>>, p1: Throwable) {
                Log.d("aaa","出错了?")
                p1.printStackTrace()
            }

        })

        val JSONWithRecService=ServiceCreator.create(JSONImageService::class.java)
        JSONWithRecService.getRecData().enqueue(object :Callback<List<rec>> {
            override fun onResponse(call: Call<List<rec>>, response: Response<List<rec>>) {
                val list=response.body()
                if (list!=null) {
                    for (i in list) {
                        recDataList.add(i)
                    }
                }
                val layoutManager=GridLayoutManager(context,3)
                RecommendedSong.layoutManager=layoutManager
                val adapter=RecommendedSongAdapter(recDataList)
                RecommendedSong.adapter=adapter
            }

            override fun onFailure(p0: Call<List<rec>>, p1: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }


}